package com.spn.blog.api.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.RequestParameterBuilder;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.schema.ScalarType;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.ParameterType;
import springfox.documentation.service.RequestParameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableOpenApi
//@Import(BeanValidatorPluginsConfiguration.class)
@Profile({"dev", "prod"})
public class Knife4jConfiguration {

    @Bean(value = "defaultApi1")
    public Docket defaultApi1() {
        Docket docket=new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //分组名称
                .groupName("文章模块")
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.spn.blog.api.controller.article"))
                .paths(PathSelectors.any())
                .build()
                .globalRequestParameters(getGlobalRequestParameters());
        return docket;
    }

    @Bean(value = "defaultApi2")
    public Docket defaultApi2() {
        Docket docket=new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //分组名称
                .groupName("系统模块")
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.spn.blog.api.controller.system"))
                .paths(PathSelectors.any())
                .build()
                .globalRequestParameters(getGlobalRequestParameters());
        return docket;
    }
//
//    @Bean(value = "defaultApi3")
//    public Docket defaultApi3() {
//        Docket docket=new Docket(DocumentationType.SWAGGER_2)
//                .apiInfo(apiInfo())
//                //分组名称
//                .groupName("模板模块")
//                .select()
//                //这里指定Controller扫描包路径
//                .apis(RequestHandlerSelectors.basePackage("com.jdc.tsdm.controller.template"))
//                .paths(PathSelectors.any())
//                .build()
//                .globalOperationParameters(getGlobalRequestParameters());
//        return docket;
//    }
//
//    @Bean(value = "defaultApi4")
//    public Docket defaultApi4() {
//        Docket docket=new Docket(DocumentationType.SWAGGER_2)
//                .apiInfo(apiInfo())
//                //分组名称
//                .groupName("课程大纲")
//                .select()
//                //这里指定Controller扫描包路径
//                .apis(RequestHandlerSelectors.basePackage("com.jdc.tsdm.controller.tsdc"))
//                .paths(PathSelectors.any())
//                .build()
//                .globalOperationParameters(getGlobalRequestParameters());
//        return docket;
//    }
//
//    @Bean(value = "defaultApi5")
//    public Docket defaultApi5() {
//        Docket docket=new Docket(DocumentationType.SWAGGER_2)
//                .apiInfo(apiInfo())
//                //分组名称
//                .groupName("教学执行计划")
//                .select()
//                //这里指定Controller扫描包路径
//                .apis(RequestHandlerSelectors.basePackage("com.jdc.tsdm.controller.tsde"))
//                .paths(PathSelectors.any())
//                .build()
//                .globalOperationParameters(getGlobalRequestParameters());
//        return docket;
//    }
//
//    @Bean(value = "defaultApi6")
//    public Docket defaultApi6() {
//        Docket docket=new Docket(DocumentationType.SWAGGER_2)
//                .apiInfo(apiInfo())
//                //分组名称
//                .groupName("All")
//                .select()
//                //这里指定Controller扫描包路径
//                //.apis(RequestHandlerSelectors.basePackage("com.jdc.tsdm.controller"))
//                .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
//                .paths(PathSelectors.any())
//                .build()
//                .globalOperationParameters(getGlobalRequestParameters());
//        return docket;
//    }

    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("SPN博客API文档")
                .description("SPN博客 RESTful APIs")
                .termsOfServiceUrl("http://spnblog.top")
                .contact(new Contact("SPN论坛","http://spnblog.top","1208900165@qq.com"))
                .version("1.0")
                .build();
    }
//    private ApiKey apiKey() {
//        return new ApiKey("BearerToken", "Authorization", "header");
//    }
//    private ApiKey apiKey1() {
//        return new ApiKey("BearerToken1", "Authorization-x", "header");
//    }

//    private SecurityContext securityContext() {
//        return SecurityContext.builder()
//                .securityReferences(defaultAuth())
//                .forPaths(PathSelectors.regex("/.*"))
//                .build();
//    }
//    private SecurityContext securityContext1() {
//        return SecurityContext.builder()
//                .securityReferences(defaultAuth1())
//                .forPaths(PathSelectors.regex("/.*"))
//                .build();
//    }

//    List<SecurityReference> defaultAuth() {
//        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
//        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
//        authorizationScopes[0] = authorizationScope;
//        return CollectionUtil.newArrayList(new SecurityReference("BearerToken", authorizationScopes));
//    }
//    List<SecurityReference> defaultAuth1() {
//        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
//        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
//        authorizationScopes[0] = authorizationScope;
//        return CollectionUtil.newArrayList(new SecurityReference("BearerToken1", authorizationScopes));
//    }

    //生成全局通用参数
/*    private List<Parameter> getGlobalRequestParameters() {
        List<Parameter> parameters = new ArrayList<>();
        parameters.add(new ParameterBuilder()
                .name(StpUtil.getTokenName())
                .description("接口校验参数")
                .modelRef(new ModelRef("String"))
                .required(false)
                .parameterType("header")
                .required(false)
                .build());
        return parameters;
    }*/

    //生成全局通用参数 v3.x
    private List<RequestParameter> getGlobalRequestParameters() {
        List<RequestParameter> parameters = new ArrayList<>();
        parameters.add(new RequestParameterBuilder()
                .name("token")
                .description("接口校验参数")
                .required(false)
                .in(ParameterType.HEADER)
                .query(q -> q.model(m -> m.scalarModel(ScalarType.STRING)))
                .required(false)
                .build());
        return parameters;
    }
}
